package com.itheima.service.impl;


import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    public List<Dept> findAll(){
        //统计部门管理各个业务层方法执行耗时
        //原始方法：在每一个业务层上也执行一个方法
//        long begin = System.currentTimeMillis();
//        List<Dept> deptList = deptMapper.findAll();
//        long end = System.currentTimeMillis();
//        log.info("执行耗时：{}",end-begin);
//        return deptList;


        return deptMapper.findAll();
    }


    public void deleteById(Integer id){
        deptMapper.deleteById(id);
    }

    public void save(Dept dept){
        //补全基本属性
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        //保存部门
        deptMapper.insert(dept);
    }

    public Dept getById(Integer id){
        return deptMapper.getById(id);
    }

    public void update(Dept dept){
        //补全基本属性：由于是修改操作，每一次修改数据，都需要更新updateTime。
        dept.setUpdateTime(LocalDateTime.now());

        //保存部门
        deptMapper.update(dept);
    }
}
